#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <cstring>
using namespace std;
void sigcb(int sig)
{
    cout << "进程收到信号:" << sig << endl;
}
int main()
{
    //signal
    // signal(2, sigcb);
    // while(1)
    // {
    //     cout << "process is running. pid: " << getpid() << endl;
    //     sleep(2);
    // }

    //sigaction
    // struct sigaction act, oact;
    // act.sa_handler = sigcb;
    // sigaction(2, &act, &oact);
    // while(1)
    // {
    //     cout << "process is running. pid: " << getpid() << endl;
    //     sleep(2);
    // }

    //sigprocmask
    sigset_t blockSig, oldBlockSig; //定义block信号集
    //将2,40信号都添加屏蔽字和自定义方法
    sigaddset(&blockSig, 2);
    sigaddset(&blockSig, 2);
    signal(2, sigcb);
    signal(40, sigcb);
    //设置用户级信号屏蔽字到内核中，让当前进程屏蔽信号
    sigprocmask(SIG_SETMASK, &blockSig, &oldBlockSig);
    while(1)
    {
        cout << "pid: " << getpid() << endl;
        sleep(2);
    }
    return 0;
}
